package com.wh.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.legend.security.Permission;
import com.legend.security.PermissionConfig;
import com.legend.security.PermissionItem;
import com.legend.utils.i18nUtils;
import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.wh.beans.LoginInfoCookie;

import com.legend.utils.ReturnResult;
import com.legend.utils.Status;

public class PermissionBo {
	Logger log = Logger.getRootLogger();

	/**
	 * 获取当前模块的权限数据
	 * 
	 * @param moduleCode
	 * @return
	 */
	public String getCurrentPermission(String moduleCode) {
		PermissionConfig config = new PermissionConfig();

		ReturnResult result = new ReturnResult();
		try {
			List<PermissionItem> lst = config.getPermissionItems(moduleCode);
			result.setMsg(i18nUtils.getVal("100001000"));
                    result.setStatus(Status.SUCCESS);
			result.setRows(lst);
		} catch (Exception e) {
			result.setMsg(i18nUtils.getVal("100002000"));
                    log.error("获取模块[" + moduleCode + "]的权限列表时出错:" + e.getMessage());
			result.setStatus(Status.ERROR);
		}
		return JSONObject.toJSONString(result);
	}

	/**
	 * 获取当前用户的所有操作权限
	 * 
	 * @return ReturnResult
	 */
	public List<Permission> getMyPermission(HttpServletRequest request) {
		PermissionConfig config = new PermissionConfig();
		LoginInfoCookie loginCookie = CommonBo.getInstance().checkLogin(request);
		List<Permission> lst = config.getPermissions();
		if (loginCookie.getIsSuper().equals(1)) {
			log.info("所有权限");
			return lst;
		} else if (loginCookie != null) {
			// Object oUserPowerMap =
			// SessionUtils.getInstance().get(ConstVar.SYSUSERPOWER_SESSIONKEY);
			// Object oRolePower = SessionUtils.getInstance().get(
			// ConstVar.SYSROLEPOWER_SESSIONKEY);

			Map<String, Integer> userPowerMap = JSONObject.parseObject(loginCookie.getUserPower(),
					new TypeReference<Map<String, Integer>>() {
					});

			Map<String, Integer> rolePowerMap = JSONObject.parseObject(loginCookie.getRolePower(),
					new TypeReference<Map<String, Integer>>() {
					});
			List<Permission> lstTmp = new ArrayList<Permission>();
			for (Permission power : lst) {
				String moduleCode = power.getModuleCode();
				int rolePowerValue = 0;
				if (rolePowerMap.containsKey(moduleCode)) {
					rolePowerValue = rolePowerMap.get(moduleCode);
				}
				int userPowerValue = 0;
				if (userPowerMap.containsKey(moduleCode)) {
					userPowerValue = userPowerMap.get(moduleCode);
				}
				List<PermissionItem> itemLst = new ArrayList<PermissionItem>();
				for (PermissionItem item : power.getItems()) {
					if ((rolePowerValue & item.getValue()) == item.getValue()) {
						itemLst.add(item);
					} else if ((userPowerValue & item.getValue()) == item.getValue()) {
						itemLst.add(item);
					}
				}
				if (itemLst.size() > 0) {
					power.setItems(itemLst);
					lstTmp.add(power);
				}
			}
			return lstTmp;

		}
			return null;
	}
}
